/**************************************************************************** **
 * Copyright (C) 2001-2020 Inhand Networks, Inc.
 **************************************************************************** **/

/* ************************************************************************** **
 *     MODULE NAME            : system
 *     LANGUAGE               : C
 *     TARGET ENVIRONMENT     : Any
 *     FILE NAME              : APLE_code.h
 *     FIRST CREATION DATE    : 2022/05/29
 * --------------------------------------------------------------------------
 *     Version                : 1.0
 *     Author                 : EExuke
 *     Last Change            : 2022/05/29
 *     FILE DESCRIPTION       : API exported to external moduels
** ************************************************************************** */
#ifndef _APLE_CODE_H_
#define _APLE_CODE_H_

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <grp.h>
#include <pwd.h>
#include <time.h>
#include <dirent.h>
#include <errno.h>
#include <termios.h>
#include <stdbool.h>
#include <fcntl.h>
#include <signal.h>
#include <setjmp.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/syslog.h>
#include <sys/param.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/msg.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/syscall.h>
#include <sys/un.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include<netinet/tcp.h>

#include "/home/xuke/bin/dbg.h"

/*------------------Macro Definition-------------------------*/
#define N_BITS    3
#define NAME_SIZE    128

#define REGU_FILE 1
#define DIR_FILE 2

#define PASSWD_LEN    8

#define THREAD_MAX_NUM  100     /* max number of thread. */
#define THREAD_DEF_NUM  20      /* by default ,number of thread. */
#define THREAD_MIN_NUM  5       /* min number of thread pool. */
/*------------------End of Macro Definition------------------*/

struct fnode {
	struct fnode *next;    //下一个成员
	char name[NAME_SIZE];  //当前成员文件名
};

typedef struct node{
	struct node *next;     //下一结点，用于构建单链结构
	unsigned int level;    //当前子目录深度，用于区分输出时的层次
	char *name;            //所在目录下的文件名，不含有上级目录，用于输出名字
	char *fullname;        //整个文件路径，用于读取属性
} filenode;

typedef struct head {
	struct node *head;    //队首指针
	struct node *rear;    //队尾指针
} headnode;


/* *ds of the every task. make all task in a single link */
typedef struct task_node {
	void *arg;                    /* fun arg. */
	void *(*fun) (void *);        /* the real work of the task. */
	pthread_t tid;                /* which thread exec this task. */
	int work_id;                  /* task id. */
	int flag;                     /* 1: assigned, 0: unassigned. */
	struct task_node *next;
	pthread_mutex_t mutex;        /* when modify this ds and exec the work,lock the task ds. */
} TASK_NODE;
/* *the ds  of the task_queue */
typedef struct task_queue {
	pthread_mutex_t mutex;
	pthread_cond_t cond;          /* when no task, the manager thread wait for ;when a new task come, signal. */
	struct task_node *head;       /* point to the task_link. */
	int number;                   /* current number of task, include unassinged and assigned but no finished. */
} TASK_QUEUE_T;
/* *the ds of every thread, make all thread in a double link queue.  */
typedef struct pthread_node {
	pthread_t tid;               /* the pid of this thread in kernel,the value is  syscall return . */
	int flag;                    /*  1:busy, 0:free. */
	struct task_node *work;      /*  if exec a work, which work. */
	struct pthread_node *next;
	struct pthread_node *prev;
	pthread_cond_t cond;        /* when assigned a task, signal this child thread by manager. */
	pthread_mutex_t mutex;
} THREAD_NODE;
/* *the ds of the thread queue */
typedef struct pthread_queue {
	int number;                  /* the number of thread in this queue. */
	struct pthread_node *head;
	struct pthread_node *rear;
	pthread_cond_t cond;        /* when no idle thread, the manager wait for ,or when a thread return with idle, signal. */
	pthread_mutex_t mutex;
} PTHREAD_QUEUE_T;
/* * the ds of send msg between server and client.  */
struct info {
	char flag;                    /* 1:file attribute; 2:file content. */
	char buf[256];                /* require file name. */
	int local_begin;              /* if get attribute, no used. */
	int length;                   /* if get attribute, no used. */
};

/*------------------API Definition---------------------------*/
void aple_code_test();
// chapter 3
int getopt_example(int argc, char **argv);
// chapter 6
int file_stat_test(const char *file_name);
int my_ls_test(int argc, const char *argv[]);
int my_tree_test(int argc, char *argv[]);
// chapter 7
int read_write_serial(int argc, char *argv[]);
int my_get_passwd(int argc, char *argv[]);
// chapter 8
int creat_daemon_test(int argc,char *argv[]);
// chapter 10
int ulararm_example(int argc, char *argv[]);
int signal_copy(int argc, char *argv[]);
// chapter 11
int msg_ipc_info(int argc, char *argv[]);
int msg_sender_exp(int argc, char *argv[]);
int msg_recevier_exp(int argc, char *argv[]);
int sem_productor(int argc,char *argv[]);
int sem_customer(int argc,char *argv[]);
int shm_sem_example_recv(int argc,char *argv[]);
int shm_sem_example_send(int argc,char *argv[]);
// chapter 12
int pthread_key_test(int argc, char *argv[]);
int pthread_cond_example(int argc, char *argv[]);
int pthread_signal(int argc,char *argv[]);
// chapter 13
int tcp_p_p_chat_server(int argc,char *argv[]);
int tcp_p_p_chat_client(int argc,char *argv[]);
// chapter 14
int tcp_select_chat_server(int argc, char *argv[]);
int tcp_select_chat_client(int argc, char *argv[]);
// chapter 15
int udp_brodcast_send(int argc, char **argv);
int udp_brodcast_recv(int argc, char **argv);
int udp_group_brodcast_send(int argc, char **argv);
int udp_group_brodcast_recv(int argc, char **argv);
int sigio_server(int argc, char *argv[]);
int sigio_client(int argc, char *argv[]);
int gethostbyname_exp(int argc, char *argv[]);
int gethostbyaddr_exp(int argc, char *argv[]);
int getaddrinfo_exp(int argc, char *argv[]);
// chapter 16
int sigpipe_server(int argc, char **argv);
int sigpipe_client(int argc, char **argv);
int multi_thread_server(int argc, char *argv[]);
int pthread_pool_server(int argc, char *argv[]);
int pthread_pool_client(int argc, char *argv[]);
// chapter 17
int socketpair_example(int argc, char *argv[]);
int socket_local_server(int argc,char *argv[]);
int socket_local_client(int argc,char *argv[]);
int send_fd_server(int argc, char *argv[]);
int send_fd_client(int argc, char *argv[]);
int raw_icmp_ping(int argc, char *argv[]);
int raw_DOS_exp(int argc,char *argv[]);

/*------------------End of API Definition--------------------*/

#endif /* End of _APLE_CODE_H_ */

